<?php

switch ($_GET['action']) {
    case 'changeset':
        $name = $_POST['name'];

        if (checkChangeset($name)) {
            if (!is_writeable(DB_CHANGESETS_PATH)) {
                DBU_Flash_Messages::instance()->setError('The Changesets Path is not writeable.');
            } else {
                if (!is_numeric($name)) {
                    DBU_Flash_Messages::instance()->setError('The Changesets Dir name must be numeric.');
                } else {
                    $path = $updater->changesetDirPath($name);
                    if (is_dir($path)) {
                        DBU_Flash_Messages::instance()->setError('The Changesets Dir exits.');
                    } else {
                        DBU_File_System::instance()->createDir($path);
                        chmod($path, 0777);
                        DBU_Flash_Messages::instance()->setConfirm('Changeset Added.', 'confirm');
                    }
                }
            }
        }
        break;

    case 'file':
        $name = $_POST['name'];
        $changeset = $_GET['changeset'];

        if (checkFile($name, $changeset)) {
            $files = $updater->getChangesetFiles($changeset);
            $number = count($files) + 1;
            $number = str_pad($number, 2, "0", STR_PAD_LEFT);
            $filepath = $updater->changesetPath($number . '_' . $name, $changeset);
            if (is_file($filepath)) {
                DBU_Flash_Messages::instance()->setError('The file name exits.');
            } else {
                file_put_contents($filepath, '');
                chmod($filepath, 0777);
                DBU_Flash_Messages::instance()->setConfirm('File Added.', 'confirm');
            }
        }
        break;

    case 'content':
        $changeset = $_GET['changeset'];
        $file = $_GET['file'];
        $content = $_POST['content'];

        if (checkFileContent($content, $file, $changeset)) {
            $filepath = $updater->changesetPath($file, $changeset);
            file_put_contents($filepath, $content);
            DBU_Flash_Messages::instance()->setConfirm('File content updated.', 'confirm');
        }
        break;

    case 'delete':
        $changeset = $_GET['changeset'];
        if (!empty($changeset)) {
            $path = $updater->changesetDirPath($changeset);
            if (!is_dir($path)) {
                DBU_Flash_Messages::instance()->setError('The Changesets dir [' . $path . '] is does not exist.');
            } else {
                DBU_File_System::instance()->removeDir($path);
                DBU_Flash_Messages::instance()->setConfirm('Changesets deleted.');
            }
        }
        break;
}


function checkChangeset($changeset) {
    global $updater;

    if (empty ($changeset)) {
        DBU_Flash_Messages::instance()->setError('The changeset name must be numeric.');
        return false;
    }

    if (!is_numeric($changeset)) {
        DBU_Flash_Messages::instance()->setError('The changeset name must be numeric.');
        return false;
    }

    $version = $updater->getVersion();
    if ($version >= $changeset) {
        DBU_Flash_Messages::instance()->setError('The changeset name must be greater than the current version.');
        return false;
    }

    return true;
}

function checkFile($file, $changeset) {
    global $updater;

    if (!checkChangeset($changeset)) {
        return false;
    }

    $path = $updater->changesetDirPath($changeset);

    if (!is_dir($path)) {
        DBU_Flash_Messages::instance()->setError('The changeset dir does not exist.');
        return false;
    }

    if (!is_writeable($path)) {
        DBU_Flash_Messages::instance()->setError('The Changesets dir [' . $path . '] is not writeable.');
        return false;
    }

    if (empty($file)) {
        DBU_Flash_Messages::instance()->setError('The filename is empty.');
    }

    return true;
}

function checkFileContent($content, $file, $changeset) {
    global $updater;

    if (!checkFile($file, $changeset)) {
        return false;
    }

    $filepath = $updater->changesetPath($file, $changeset);

    if (!is_file($filepath)) {
        DBU_Flash_Messages::instance()->setError('The file path [' . $filepath . '] does not exist.');
        return false;
    }

    if (!is_writeable($filepath)) {
        DBU_Flash_Messages::instance()->setError('The file [' . $filepath . '] is not writeable.');
        return false;
    }

    if (empty($content)) {
        DBU_Flash_Messages::instance()->setError('The content is empty.');
        return false;
    }
    
    return true;
}
